import { route, GET, POST } from 'awilix-express';

@route('/ips')
export default class IpsApi {

  constructor({ ipsService }) {
    this.ipsService = ipsService;
  }

  @route('/getIpsAll')
  @POST()
  async getIpsAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.ipsService.getIpsAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getIpslist')
  @POST()
  async getIpslist(req, res) {
    const { SERVER_URL, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.ipsService.getIpslist({SERVER_URL, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  @route('/addIps')
  @POST()
  async addIps(req, res) {
    const {
      SERVER_URL,
      mark
     } = req.body;
    const { success, message } = await this.ipsService.addIps({
      SERVER_URL,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delIps')
  @POST()
  async delIps(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.ipsService.delIps({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/updateIps')
  @POST()
  async updateIps(req, res) {
    const {
      id,
      SERVER_URL
    } = req.body;
    const { success, message } = await this.ipsService.updateIps({
      id,
      SERVER_URL
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}